package example.hellosecurity.controller;

import example.hellosecurity.session.LoginAccountModel;
import example.hellosecurity.utils.JwtUtil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 *
 * @author ：FuXx
 * @className : example.hellosecurity.controller.TestController
 * @since ：2024/8/19
 */
@RestController
@RequestMapping("/usr")
public class TestController {

    @GetMapping("/login")
    public String login() {
        Map<String, Object> claims = new HashMap<>(2);
        claims.put("name", "FuXx");
        claims.put("id", 1);
        return JwtUtil.createJwt(Date.from(LocalDateTime
                .now()
                .plusMinutes(10)
                .atZone(ZoneId.systemDefault())
                .toInstant()), claims, JwtUtil.SIGNING_KEY);
    }

    @GetMapping("/test")
    public String test() {
        LoginAccountModel accountModel = (LoginAccountModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(accountModel);
        return "hello test";
    }
}
